package com.lanswon.controller.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.entity.ArticleVO;
import com.lanswon.entity.CommonProblem;
import com.lanswon.service.CommonProblemService;

@Controller
@RequestMapping("app/commonProblem")
public class AppCommonProblemController {
	
	@Autowired
	private CommonProblemService commonProblemService;
	
	@RequestMapping("pageList")
	@ResponseBody
	public Map<String, Object> cpPageList(HttpServletRequest request,HttpServletResponse response,Integer currentPage,
			Integer pageNum,String problemName,Integer problemType){
		Map<String, Object> viewMap = new HashMap<>();
		Map<String, Object> accessMap = new HashMap<>();
		List<CommonProblem> pageList = null;
		int code = 0;
		String message = "查询成功";
		try {
			if(currentPage==null) {
				currentPage = 1;
			}
			if(pageNum==null) {
				pageNum = 10;
			}
			accessMap.put("currentResult", (currentPage-1)*pageNum);
			accessMap.put("endResult", pageNum);
			accessMap.put("problemType", problemType);
			accessMap.put("problemName", problemName);
			//Integer count = commonProblemService.pageListCount(accessMap);
			pageList = commonProblemService.pageList(accessMap);
			//viewMap.put("totalPages", count!=null?count%pageNum>0?(count/pageNum)+1:count/pageNum:0);
		} catch (Exception e) {
			e.printStackTrace();
			code = 2;
			message = "查询失败";
		}
		viewMap.put("data", pageList);
		viewMap.put("code", code);
		viewMap.put("message", message);
		return viewMap;
	}
	
	
	@RequestMapping("detail")
	@ResponseBody
	public Map<String, Object> cpdetail(HttpServletRequest request,HttpServletResponse response,Long id){
		Map<String, Object> viewMap = new HashMap<>();
		Map<String, Object> accessMap = new HashMap<>();
		List<CommonProblem> cpList = new ArrayList<>();
		int code = 0;
		String message = "查询成功";
		CommonProblem commonProblem = null;
		try {
			if(id!=null) {
				accessMap.put("articleId", id);
				commonProblem = commonProblemService.findCommonProblemById(id);
				cpList.add(commonProblem);
			}
		} catch (Exception e) {
			e.printStackTrace();
			message = "查询失败";
		}
		viewMap.put("data", cpList);
		viewMap.put("code", code);
		viewMap.put("message", message);
		return viewMap;
	}
	
}
